package com.cash.channel.svc.service.common;


import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.repay.RepayPlanModel;
import com.cash.channel.api.model.request.RepayPlanListRequest;
import com.cash.channel.api.model.response.RepayPlanListResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.svc.logic.RepayLogic;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.service.base.RepayPlanList;
import com.google.common.collect.Lists;
import geex.core.service.LoanService;
import geex.core.service.model.loan.Loan;
import geex.core.service.model.loan.RepayPlan;
import geex.trans.req.LoanMainCardReq;
import geex.trans.rsp.LoanMainCardRsp;
import geex.trans.service.TransCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.查询还款计划)
public class CommonRepayPlanList implements RepayPlanList {

    @Autowired
    private LoanService loanService;

    @Autowired
    private RepayLogic repayLogic;

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Autowired
    private TransCardService transCardService;

    @Override
    public ApiResponseModel<RepayPlanListResponse> getRepayPlanList(ApiRequestModel apiRequestModel) {
        try {
            RepayPlanListRequest repayPlanListRequest = JSON.parseObject(apiRequestModel.getData(), RepayPlanListRequest.class);

            List<RepayPlan> repayPlanList = new ArrayList<>();
            try {
                Loan loan = loanService.findLoanByExtId(repayPlanListRequest.getWithdrawId());
                repayPlanList = loanService.getRepayPlanByExtId(repayPlanListRequest.getWithdrawId());
                if (repayPlanList.isEmpty()) {
                    // FIXME: 2019/11/4 earlyRepay bankName
                    return new ApiResponseModel<>(true, 0, "暂无还款计划", new RepayPlanListResponse(repayPlanListRequest.getOrderNo(),
                            0, loan.getBankCode(), "",repayPlanListRequest.getWithdrawId(),loan.getBankCard()));
                }
                List<RepayPlanModel> repayPlanModelList = repayLogic.getRepayPlan(repayPlanListRequest.getWithdrawId());
                // FIXME: 2019/11/4 earlyRepay bankName
                return new ApiResponseModel<>(true, 0, "", new RepayPlanListResponse(repayPlanListRequest.getOrderNo(),
                        0, loan.getBankCode(), "", loan.getBankCard(),repayPlanListRequest.getWithdrawId(), repayPlanModelList));
            } catch (CashChannelException e) {
                log.error("getRepayPlanByExtId error:" + e.getErrMessage(), e);
                return new ApiResponseModel<>(false, -1, e.getErrMessage(), null);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getMessage(), null);
        }
    }
}
